using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VRFID
    {
        public DataRow Convert2RFID(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryRFID(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["RFIMANDANT"] = " ".PadRight(2 ); 
         dwReturn["RFIID"] = " ".PadRight(32 ); 
         dwReturn["RFIAENDZL"] = " ".PadRight(0 ); 
         dwReturn["RFIAENDBEN"] = " ".PadRight(8 ); 
         dwReturn["RFIAENDDAT"] = " ".PadRight(0 ); 
         dwReturn["RFINEUBEN"] = " ".PadRight(8 ); 
         dwReturn["RFINEUDAT"] = " ".PadRight(0 ); 
         dwReturn["RFISTAKZ"] = " ".PadRight(1 ); 
         dwReturn["RFITYP"] = " ".PadRight(1 ); 
         dwReturn["RFIOBJECT"] = " ".PadRight(20 ); 
         dwReturn["RFIMNRKRS"] = " ".PadRight(2 ); 
         dwReturn["RFIMLFDNR"] = " ".PadRight(4 ); 
         dwReturn["RFIMPOS"] = " ".PadRight(2 ); 
         dwReturn["RFIANZ"] = " ".PadRight(0 ); 
         dwReturn["RFIZL"] = " ".PadRight(0 ); 
         dwReturn["RFIFILLER"] = " ".PadRight(102 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapRFID(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveRFID(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryRFID(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapRFID(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertRFID(dwLocal, ITXDBVERSION);
            }
            UpdateRFID(dwLocal);
        }
        public void InsertRFID(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO RFID(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryRFID(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM RFID WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM RFID  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "RFID";
            Return = Result;
            return Return;
        }
        public void DeleteRFID(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM RFID   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateRFID(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE RFID SET "
                    +"RFIMANDANT= :RFIMANDANT,"
                    +"RFIID= :RFIID,"
                    +"RFIAENDZL= :RFIAENDZL,"
                    +"RFIAENDBEN= :RFIAENDBEN,"
                    +"RFIAENDDAT= :RFIAENDDAT,"
                    +"RFINEUBEN= :RFINEUBEN,"
                    +"RFINEUDAT= :RFINEUDAT,"
                    +"RFISTAKZ= :RFISTAKZ,"
                    +"RFITYP= :RFITYP,"
                    +"RFIOBJECT= :RFIOBJECT,"
                    +"RFIMNRKRS= :RFIMNRKRS,"
                    +"RFIMLFDNR= :RFIMLFDNR,"
                    +"RFIMPOS= :RFIMPOS,"
                    +"RFIANZ= :RFIANZ,"
                    +"RFIZL= :RFIZL,"
                    +"RFIFILLER= :RFIFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("RFIMANDANT", OracleType.NVarChar).Value = dwData["RFIMANDANT"].ToString(); 
          or1.Parameters.Add("RFIID", OracleType.NVarChar).Value = dwData["RFIID"].ToString(); 
          or1.Parameters.Add("RFIAENDZL", OracleType.NVarChar).Value = dwData["RFIAENDZL"].ToString(); 
          or1.Parameters.Add("RFIAENDBEN", OracleType.NVarChar).Value = dwData["RFIAENDBEN"].ToString(); 
          or1.Parameters.Add("RFIAENDDAT", OracleType.NVarChar).Value = dwData["RFIAENDDAT"].ToString(); 
          or1.Parameters.Add("RFINEUBEN", OracleType.NVarChar).Value = dwData["RFINEUBEN"].ToString(); 
          or1.Parameters.Add("RFINEUDAT", OracleType.NVarChar).Value = dwData["RFINEUDAT"].ToString(); 
          or1.Parameters.Add("RFISTAKZ", OracleType.NVarChar).Value = dwData["RFISTAKZ"].ToString(); 
          or1.Parameters.Add("RFITYP", OracleType.NVarChar).Value = dwData["RFITYP"].ToString(); 
          or1.Parameters.Add("RFIOBJECT", OracleType.NVarChar).Value = dwData["RFIOBJECT"].ToString(); 
          or1.Parameters.Add("RFIMNRKRS", OracleType.NVarChar).Value = dwData["RFIMNRKRS"].ToString(); 
          or1.Parameters.Add("RFIMLFDNR", OracleType.NVarChar).Value = dwData["RFIMLFDNR"].ToString(); 
          or1.Parameters.Add("RFIMPOS", OracleType.NVarChar).Value = dwData["RFIMPOS"].ToString(); 
          or1.Parameters.Add("RFIANZ", OracleType.NVarChar).Value = dwData["RFIANZ"].ToString(); 
          or1.Parameters.Add("RFIZL", OracleType.NVarChar).Value = dwData["RFIZL"].ToString(); 
          or1.Parameters.Add("RFIFILLER", OracleType.NVarChar).Value = dwData["RFIFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
